package com.qc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.enity.*;
import com.qc.mapper.LoginMapper;
import com.qc.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper,Login> implements ILoginService   {
    @Autowired
    private LoginMapper loginMapper ;
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public Login loginUser(String username, String password) {
        QueryWrapper<Login> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        List<Login> logins = loginMapper.selectList(queryWrapper);
        System.out.println(logins);
        // 若找到用户记录
        if (!logins.isEmpty()) {
            Login user = logins.get(0);
            // 验证密码
            if (passwordEncoder.matches(password, user.getUserPassword())) {
                return user;
            }
        }
        return null;
    }

    @Override
    public int register(String username, String password,String Bid) {

        Login login = new Login();
        login.setUserName(username);
        QueryWrapper<Login> queryWrapper=new QueryWrapper();
        Integer count = loginMapper.selectCount(queryWrapper);
        count = count  + 1 ;
        login.setUserId(count);
        System.out.println(username);
        // 对密码进行加密
        String encryptedPassword = passwordEncoder.encode(password);
        login.setUserPassword(encryptedPassword);
//        login.setUserPassword(password);
        login.setUserBid(Bid);
        int insert = loginMapper.insert(login);
        return insert;
    }

    @Override
    public int update(String username, String prePassword, String password) {
        // 进行参数有效性检查
        if (username == null || username.isEmpty() || prePassword == null || prePassword.isEmpty() || password == null || password.isEmpty()) {
            throw new IllegalArgumentException("用户名、原密码和新密码都不能为空");
        }

        QueryWrapper<Login> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        List<Login> logins = loginMapper.selectList(queryWrapper);

        if (logins.isEmpty()) {
            return 0;
        }
        // 假设只更新第一条匹配的记录
        Login targetLogin = logins.get(0);
        // 验证原密码是否正确
        if (!passwordEncoder.matches(prePassword, targetLogin.getUserPassword())) {
            // 原密码不正确
            return 0;
        }
        System.out.println("正确了");
        // 对新密码进行加密
        String encryptedPassword = passwordEncoder.encode(password);
        targetLogin.setUserPassword(encryptedPassword);
        // 使用 ID 进行更新操作，更高效准确
        QueryWrapper<Login> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", targetLogin.getUserId());
        return loginMapper.update(targetLogin, queryWrapper2);
    }

    @Override
    public List<Login> selectFamilyByUserBid(String userBid) {
        Login login = new Login();
        login.setUserBid(userBid);
        QueryWrapper<Login> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_bid", userBid);
        List<Login> loginList = loginMapper.selectList(queryWrapper);
        return  loginList;
    }

    @Override
    public List<Login> selectFamilyByParams(Login login) {
        QueryWrapper<Login> queryWrapper = new QueryWrapper<>(login);
        List<Login> loginList = loginMapper.selectList(queryWrapper);
        return  loginList;
    }
}
